package luohuayu.CatPixelmonSync;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;

import com.pixelmonmod.pixelmon.api.economy.IPixelmonBankAccount;

import net.milkbowl.vault.economy.AbstractEconomy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;

public class EconomyHook extends AbstractEconomy {
    private String name = "CatPixelmonSync";

    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public double getBalance(String playerName) {
        IPixelmonBankAccount account = CatPixelmonSync.instance.getPixelmonBankAccount(playerName);
        return account != null ? account.getMoney() : 0;
    }

    @Override
    public EconomyResponse withdrawPlayer(String playerName, double amount) {
        IPixelmonBankAccount account = CatPixelmonSync.instance.getPixelmonBankAccount(playerName);
        if (account == null) return new EconomyResponse(0, 0, ResponseType.FAILURE, "");

        double balance = account.getMoney();
        if (amount < 0) {
            return new EconomyResponse(0, balance, ResponseType.FAILURE, "");
        } else if (balance - amount < 0) {
            return new EconomyResponse(0, balance, ResponseType.FAILURE, "");
        }
        if (CatPixelmonSync.instance.changeMoney(account, (int) (amount * -1))) {
            return new EconomyResponse(amount, account.getMoney(), ResponseType.SUCCESS, "");
        } else {
            return new EconomyResponse(0, balance, ResponseType.FAILURE, "");
        }
    }

    @Override
    public EconomyResponse depositPlayer(String playerName, double amount) {
        IPixelmonBankAccount account = CatPixelmonSync.instance.getPixelmonBankAccount(playerName);
        if (account == null) return new EconomyResponse(0, 0, ResponseType.FAILURE, "");

        double balance = account.getMoney();
        if (amount < 0) {
            return new EconomyResponse(0, balance, ResponseType.FAILURE, "");
        }
        if (CatPixelmonSync.instance.changeMoney(account, (int) amount)) {
            return new EconomyResponse(amount, account.getMoney(), ResponseType.SUCCESS, "");
        } else {
            return new EconomyResponse(0, account.getMoney(), ResponseType.FAILURE, "");
        }
    }

    @Override
    public String currencyNamePlural() {
        return "";
    }

    @Override
    public String currencyNameSingular() {
        return "";
    }

    @Override
    public String format(double amount) {
        return String.valueOf(amount);
    }

    @Override
    public EconomyResponse createBank(String name, String player) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse deleteBank(String name) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse bankHas(String name, double amount) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse bankWithdraw(String name, double amount) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse bankDeposit(String name, double amount) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public boolean has(String playerName, double amount) {
        return getBalance(playerName) >= amount;
    }

    @Override
    public EconomyResponse isBankOwner(String name, String playerName) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse isBankMember(String name, String playerName) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public EconomyResponse bankBalance(String name) {
        return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "not support bank accounts!");
    }

    @Override
    public List<String> getBanks() {
        return new ArrayList<String>();
    }

    @Override
    public boolean hasBankSupport() {
        return false;
    }

    @Override
    public boolean hasAccount(String playerName) {
        return CatPixelmonSync.instance.getPixelmonBankAccount(playerName) != null;
    }

    @Override
    public boolean createPlayerAccount(String playerName) {
        return false;
    }

    @Override
    public int fractionalDigits() {
        return -1;
    }

    @Override
    public boolean hasAccount(String playerName, String worldName) {
        return hasAccount(playerName);
    }

    @Override
    public double getBalance(String playerName, String world) {
        return getBalance(playerName);
    }

    @Override
    public boolean has(String playerName, String worldName, double amount) {
        return has(playerName, amount);
    }

    @Override
    public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
        return withdrawPlayer(playerName, amount);
    }

    @Override
    public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
        return depositPlayer(playerName, amount);
    }

    @Override
    public boolean createPlayerAccount(String playerName, String worldName) {
        return createPlayerAccount(playerName);
    }
}
